//
//  DragableView.m
//  TanTanDemo
//
//  Created by wei1 on 2016/12/2.
//  Copyright © 2016年 qge. All rights reserved.
//

#import "DragableView.h"

@interface DragableView()
//界面中点
@property(nonatomic) CGPoint centerPoint;
//likeicon和dislikeIcon
@property (nonatomic) UIImageView* likeIconV;
@property (nonatomic) UIImageView* dislikeIconV;
@property(nonatomic)CGFloat touchInY;
@end

@implementation DragableView
#pragma mark 初始化方法,View需要外面传入
-(instancetype)initWithFrame:(CGRect)frame andView:(UIView *)view{
    self = [super initWithFrame:frame];
    if (self){
        [self setupView];
        [self addSubview:view];
        [self setUpLogo];
        self.centerPoint = self.center;
        [self setUpGestureRecognizer];
    }
    return self;
}
#pragma mark 设置View
-(void)setupView
{
    self.layer.cornerRadius = 5;
    self.layer.shadowRadius = 4;
    self.layer.shadowOpacity = 0.2;
    self.layer.shadowOffset = CGSizeMake(2, 2);
    self.backgroundColor = [UIColor whiteColor];
}

#pragma mark 设置icon
-(void)setUpLogo{
    UIImage *likeIcon = [UIImage imageNamed:@"home-card-like-icon_80x81_"];
    UIImage *dislikeIcon = [UIImage imageNamed:@"home-card-dislike-icon_80x80_"];
    UIImageView *likeIconV = [[UIImageView alloc]initWithFrame:CGRectMake(likeIconMergin, likeIconMergin, likeIconWH, likeIconWH)];
    UIImageView *disLikeIconV= [[UIImageView alloc]initWithFrame:CGRectMake(ViewWidth - likeIconMergin - likeIconWH, likeIconMergin, likeIconWH, likeIconWH)];
    [disLikeIconV setImage:dislikeIcon];
    [likeIconV setImage:likeIcon];
    likeIconV.alpha = 0;
    disLikeIconV.alpha = 0;
    _dislikeIconV = disLikeIconV;
    _likeIconV = likeIconV;
    [self addSubview:likeIconV];
    [self addSubview:disLikeIconV];
}

-(void)setUpGestureRecognizer{
    UIPanGestureRecognizer *recongizer = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:recongizer];
}

-(void)pan:(UIPanGestureRecognizer *)recongizer{
    CGFloat xOffset = [recongizer translationInView:self].x;
    CGFloat yOffset = [recongizer translationInView:self].y;
    
    switch (recongizer.state) {
        case UIGestureRecognizerStateBegan:
            self.touchInY = [recongizer locationInView:self].y;
            break;
        case UIGestureRecognizerStateChanged:{
            
            CGFloat Strenge = fabs(xOffset) < MAXStrenge ? fabs(xOffset):MAXStrenge;
            //逆时针旋转的条件
            if (xOffset <= 0 ^ self.touchInY>self.centerPoint.y){
                Strenge = - Strenge;
            }
            CGFloat scale = (ScaleValue - fabs(Strenge)/MAXStrenge)/ScaleValue;
            CGFloat rotate = Strenge/MAXStrenge/RotateValue*M_PI;

            CGAffineTransform scaleT = CGAffineTransformMakeScale(scale, scale);
            
            self.center = CGPointMake(self.centerPoint.x + xOffset, self.centerPoint.y + yOffset);
            CGAffineTransform rotateT = CGAffineTransformMakeRotation(rotate);
            CGAffineTransform concatT = CGAffineTransformConcat(scaleT, rotateT);
            self.transform = concatT;
            UIImageView *chooseIcon = xOffset > 0 ? _likeIconV:_dislikeIconV;
            UIImageView *anotherIcon = xOffset < 0 ? _likeIconV:_dislikeIconV;

            [chooseIcon setAlpha:fabs(Strenge)/MAXStrenge];
            [anotherIcon setAlpha:0];
            
            break;
        }
        case UIGestureRecognizerStateEnded:{
            if (self.center.x < likeMergin){
                [self dragToLeft];
            }else if([UIScreen mainScreen].bounds.size.width - self.center.x < likeMergin){
                [self dragToRight];
            }else{
                [UIView animateWithDuration:0.3 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseInOut animations:^{
                    self.center = self.centerPoint;
                    self.transform = CGAffineTransformIdentity;
                    [self.likeIconV setAlpha:0];
                    [self.dislikeIconV setAlpha:0];
                } completion:nil];
            }
            break;
        }
        default: break;
    }
}

-(void)dragToLeft{
    _dislikeIconV.alpha = 1.0;
    CGFloat scale = (ScaleValue - 1.0)/ScaleValue;
    CGAffineTransform scaleT = CGAffineTransformMakeScale(scale, scale);
    CGFloat rotate = -1.0/RotateValue*M_PI;
    CGAffineTransform rotateT = CGAffineTransformMakeRotation(rotate);
    CGAffineTransform concatT = CGAffineTransformConcat(scaleT, rotateT);
    [UIView animateWithDuration:0.3 animations:^{
        self.center = CGPointMake(-[UIScreen mainScreen].bounds.size.width/2, self.center.y);
        self.transform = concatT;
    } completion:^(BOOL finished) {
        [_delegate dragableViewDragToLeft:self];
        [self removeFromSuperview];
    }];
}
-(void)dragToRight{
    _likeIconV.alpha = 1;
    CGFloat scale = (ScaleValue - 1.0)/ScaleValue;
    CGAffineTransform scaleT = CGAffineTransformMakeScale(scale, scale);
    CGFloat rotate = 1.0/RotateValue*M_PI;
    CGAffineTransform rotateT = CGAffineTransformMakeRotation(rotate);
    CGAffineTransform concatT = CGAffineTransformConcat(scaleT, rotateT);
    [UIView animateWithDuration:0.3 animations:^{
        self.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2*3, self.center.y);
        self.transform = concatT;
    } completion:^(BOOL finished) {
        [_delegate dragableViewDragToRight:self];
        [self removeFromSuperview];
    }];
}
@end
